package com.lazason.wib4android;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.lazason.wib4android.AsyncImageLoader.BitmapCallback;

public class ImageBrowserActivity extends Activity {
	String original_pic;
	String id;
	AsyncImageLoader asyncImageLoader;
	ImageView originalPicture;
	Bitmap bitmap;
	private static final String TAG = "ScaleDetector";
	// private ImageView image;
	private float mScaleFactor = 1f;
	private Matrix mMatrix = new Matrix();
	private ProgressDialog m_ProgressDialog = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.d("ImageBrowserActivity", "onCreate");
		setContentView(R.layout.imagebrowser);
		Log.d("ImageBrowserActivity", "setContentView");
		id = this.getIntent().getStringExtra("id");
		original_pic = this.getIntent().getStringExtra("original_pic");
		asyncImageLoader = new AsyncImageLoader();
		originalPicture = (ImageView) findViewById(R.id.originalImage);
		m_ProgressDialog = ProgressDialog.show(this, "Please wait...",
				"loading data ...", true, true);
		// 设置ProgressDialog 是否可以按退回按键取消
		m_ProgressDialog.setCancelable(true);
		
		 originalPicture.setOnTouchListener(new ViewOnThouchListener(originalPicture,this));
		// originalPicture.setOnTouchListener(new OnTouchListener(){
		//
		// @Override
		// public boolean onTouch(View paramView, MotionEvent paramMotionEvent)
		// {
		// // TODO Auto-generated method stub
		// if(paramView!=null){
		// if(paramView instanceof ImageView){
		// //if((ImageView)paramView.)
		// }
		// }
		// return false;
		// }
		//			
		// });
		// originalPicture.setOnTouchListener(new ViewOnThouchListener
		// (originalPicture));
		Log.d("ImageBrowserActivity", "findViewById");
		//originalPicture.setScaleType(ImageView.ScaleType.CENTER);
		originalPicture.setTag(id);
		// originalPicture.setImageResource(R.drawable.icon);

		Log.d("ImageBrowserActivity", "setImageResource");
		bitmap = asyncImageLoader.loadBitMap(this, original_pic, id,
				Thread.MAX_PRIORITY, new BitmapCallback() {
					public void bitmapLoaded(Bitmap bitmap, String imageUrl,
							String id) {
						ImageView imageViewByTag = (ImageView) originalPicture
								.findViewWithTag(id);
						m_ProgressDialog.cancel();
						m_ProgressDialog.dismiss();
						if (imageViewByTag != null && bitmap != null) { // 防止图片url获取不到图片是，占位图片不见了的情况
						
							imageViewByTag.setImageBitmap(bitmap); 

						} else {
							 AlertDialog.Builder builder = new Builder(ImageBrowserActivity.this);
							  builder.setMessage("遇到问题，返回么？");
							  builder.setTitle("提示");
							  builder.setPositiveButton("确认", new OnClickListener() {
							   @Override
							   public void onClick(DialogInterface dialog, int which) {
							    dialog.dismiss();
							    ImageBrowserActivity.this.finish(); 
							   }
							  });
							  builder.create().show();
						}
					}
				});
		if (bitmap != null) {
			m_ProgressDialog.cancel();
			m_ProgressDialog.dismiss();
			originalPicture.setImageBitmap(bitmap);
//			originalPicture.setOnTouchListener(new OnTouchListener() {
//
//				@Override
//				public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
//					// TODO Auto-generated method stub
//					Log.v(TAG, "in onTouchEvent");
//					return mScaleDetector.onTouchEvent(paramMotionEvent);
//					
//				}
//
//			});
		}
		
	}

	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		this.finish();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		if (m_ProgressDialog != null) {
			m_ProgressDialog.cancel();
			m_ProgressDialog.dismiss();
		}
		if (originalPicture.getDrawable() != null) {
			if (originalPicture.getDrawable() instanceof BitmapDrawable) {
				BitmapDrawable bd = (BitmapDrawable) originalPicture
						.getDrawable();
				originalPicture.setImageDrawable(null);
				originalPicture.setBackgroundDrawable(null);

				bd.setCallback(null);
				if (bd.getBitmap() != null && !bd.getBitmap().isRecycled()) {
					bd.getBitmap().recycle();
				}
			}
		}

		super.onDestroy();

	}

	// @Override
	// public boolean onTouchEvent(MotionEvent ev) {
	// Log.v(TAG, "in onTouchEvent");
	// // Give all events to ScaleGestureDetector
	// mScaleDetector.onTouchEvent(ev);
	// return true;
	// }
	//		
	// }
	
}
